package com.offcn.pay.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.offcn.pay.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class PayServiceImpl implements PayService {
    // 注入配置类对象
    @Autowired
    private AlipayClient alipayClient;

    // 异步通知来实现扫码支付
    @Override
    public Map createSeckillOrderQrCode(Map params) {
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest (); //创建API对应的request类
        String str = "{"   +
                "\"out_trade_no\":\""  + params.get("out_trade_no") + "\"," + //商户订单号
                "\"total_amount\":\"" + params.get("total_amount") + "\","   +
                "\"notify_url\":\"http://345p4ko1np.xuduan.vip/pay/notify\"," +
                "\"body\":\"queue=" + params.get("queue")  // 消息队列
                + "&username=" + params.get("username")  // username
                + "&exchange=" + params.get("exchange")  // 交换机
                + "&routing=" + params.get("routing")  // 路由
                + "&totalAmount=" + params.get("total_amount")  // 总支付金额
                + "\"," +
                "\"subject\":\"dongyimai\","   +
                "\"store_id\":\"NJ_001\","   +
                "\"timeout_express\":\"90m\"}";
        System.out.println("--------------------------");
        System.out.println(str);
        System.out.println("--------------------------");
        request . setBizContent ( str ); //订单允许的最晚付款时间
        try {
            AlipayTradePrecreateResponse response = alipayClient.execute (request);
            System.out.print( response.getBody ());
            Map map = new HashMap<>();
            if (response.getCode().equals("10000")){
                map.put("qrcode",response.getQrCode()); // 二维码连接
                map.put("out_trade_no",response.getOutTradeNo()); // 订单号
                map.put("total_amount",params.get("total_amount")); // 支付金额
                return  map;
            }
            return map;

        } catch (AlipayApiException e) {
            e.printStackTrace();
            return null;
        }
    }
    @Override
    public Map createQrCode(String out_trade_no, String total_amount) {
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest (); //创建API对应的request类
        request . setBizContent ( "{"   +
                "\"out_trade_no\":\""  + out_trade_no + "\"," + //商户订单号
                "\"total_amount\":\"" + total_amount + "\","   + // 订单金额
                "\"subject\":\"dongyimai\","   +
                "\"store_id\":\"NJ_001\","   +
                "\"timeout_express\":\"90m\"}" ); //订单允许的最晚付款时间
        try {
            AlipayTradePrecreateResponse response = alipayClient.execute (request);
            System.out.print( response.getBody ());
            Map map = new HashMap<>();
            if (response.getCode().equals("10000")){
                map.put("qrcode",response.getQrCode()); // 二维码
                map.put("out_trade_no",response.getOutTradeNo()); // 订单号
                map.put("total_amount",total_amount); // 支付金额
                return  map;
            }
            return map;
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Map queryPayStatus(String out_trade_no) {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();//创建API对应的request类
        request.setBizContent("{" +
                "\"out_trade_no\":\"" + out_trade_no + "\"}"); //设置业务参数
        try {
            AlipayTradeQueryResponse response = alipayClient.execute(request);//通过alipayClient调用API，获得对应的response类
            System.out.print(response.getBody());
            Map map = new HashMap();
            map.put("out_trade_no",response.getOutTradeNo());// 订单编号
            map.put("trade_status",response.getTradeStatus());// 支付状态
            map.put("trade_no",response.getTradeNo()); // 支付宝内部的交易Id
            return map;
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return null;
        }
    }


}
